import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store/store';
Vue.use(Router);
const whiteList = ['/register', '/forget', '/login', '/agreement']; // 路由白名单
const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/agreement',
    name: 'agreement',
    component: () => import('@/views/loginRegister/agreement'),
    meta: { title: '用户协议' }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/loginRegister/login'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/loginRegister/register'),
    meta: { title: '注册' }
  },
  {
    path: '/forget',
    name: 'forget',
    component: () => import('@/views/loginRegister/forget'),
    meta: { title: '忘记密码' }
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('@/views/layout/index'),
    children: [
      {
        path: 'orderManagement',
        name: 'orderManagement',
        component: () => import('@/views/orderManagement/index'),
        meta: { title: '订单管理' }
      },
      {
        path: 'orderInfo',
        name: 'orderInfo',
        component: () => import('@/views/orderManagement/orderInfo'),
        meta: { title: '订单信息' }
      },
      {
        path: 'chooseCustomer',
        name: 'chooseCustomer',
        component: () => import('@/views/orderManagement/chooseCustomer'),
        meta: { title: '选择客户' }
      },
      {
        path: 'chooseProduct',
        name: 'chooseProduct',
        component: () => import('@/views/orderManagement/chooseProduct'),
        meta: { title: '选择产品' }
      },
      {
        path: 'stockList',
        name: 'stockList',
        component: () => import('@/views/orderManagement/stockList'),
        meta: { title: '库存列表' }
      },
      {
        path: 'stockManagement',
        name: 'stockManagement',
        component: () => import('@/views/stockManagement/index'),
        meta: { title: '库存管理' }
      },
      {
        path: 'receivableManagement',
        name: 'receivableManagement',
        component: () => import('@/views/receivableManagement/index'),
        meta: { title: '应收管理' }
      },
      {
        path: 'supplier',
        name: 'supplier',
        component: () => import('@/views/supplier/index'),
        meta: { title: '供应商' }
      },
      {
        path: 'company',
        name: 'company',
        component: () => import('@/views/company/index'),
        meta: { title: '公司' }
      },
      {
        path: 'employeeInfo',
        name: 'employeeInfo',
        component: () => import('@/views/company/employeeInfo'),
        meta: { title: '员工信息' }
      },
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/goods/index'),
        meta: { title: '商品列表' }
      },
      {
        path: 'productAudit',
        name: 'productAudit',
        component: () => import('@/views/product/index'),
        meta: { title: '产品列表' }
      },
      {
        path: 'customer',
        name: 'customer',
        component: () => import('@/views/customer/index'),
        meta: { title: '客户' }
      },
      {
        path: 'researchGroup',
        name: 'researchGroup',
        component: () => import('@/views/customer/researchGroup'),
        meta: { title: '课题组' }
      },
      {
        path: 'purchaser',
        name: 'purchaser',
        component: () => import('@/views/customer/purchaser'),
        meta: { title: '采购人' }
      },
      {
        path: 'specifications',
        name: 'specifications',
        component: () => import('@/views/specifications/index'),
        meta: { title: '规格' }
      },
      {
        path: 'measurement',
        name: 'measurement',
        component: () => import('@/views/measurement/index'),
        meta: { title: '计量单位' }
      },
      {
        path: 'orderExportList',
        name: 'orderExportList',
        component: () => import('@/views/orderExportList/index'),
        meta: { title: '订单导出列表' }
      },
      {
        path: 'receiveExportList',
        name: 'receiveExportList',
        component: () => import('@/views/receiveExportList/index'),
        meta: { title: '出库单导出列表' }
      },
      {
        path: 'ticketExportList',
        name: 'ticketExportList',
        component: () => import('@/views/ticketExportList/index'),
        meta: { title: '开票信息模板导出列表' }
      },
      {
        path: 'stockExportList',
        name: 'stockExportList',
        component: () => import('@/views/stockExportList/index'),
        meta: { title: '库存导出列表' }
      },
      {
        path: 'ordinaryAdmin',
        name: 'ordinaryAdmin',
        component: () => import('@/views/accountManagement/ordinaryAdmin'),
        meta: { title: '用户列表' }
      },
      {
        path: 'superAdmin',
        name: 'superAdmin',
        component: () => import('@/views/accountManagement/superAdmin'),
        meta: { title: '用户列表' }
      },
      {
        path: 'modifyPassword',
        name: 'modifyPassword',
        component: () => import('@/views/loginRegister/modifyPassword'),
        meta: { title: '修改密码' }
      },
      {
        path: 'qualificationReview',
        name: 'qualificationReview',
        component: () => import('@/views/qualificationReview/index'),
        meta: { title: '资质审核' }
      },
      {
        path: 'orderReview',
        name: 'orderReview',
        component: () => import('@/views/orderReview/index'),
        meta: { title: '会员审核' }
      },
      {
        path: 'feedback',
        name: 'feedback',
        component: () => import('@/views/feedback/index'),
        meta: { title: '意见反馈' }
      },
      {
        path: 'feedbackList',
        name: 'feedbackList',
        component: () => import('@/views/feedbackList/index'),
        meta: { title: '反馈列表' }
      },
      {
        path: 'messageList',
        name: 'messageList',
        component: () => import('@/views/messageList/index'),
        meta: { title: '消息中心' }
      },
      {
        path: 'payOrder',
        name: 'payOrder',
        component: () => import('@/views/payOrder/index'),
        meta: { title: '支付订单' }
      },
      { path: 'redPacketConfig',
        name: 'redPacketConfig',
        component: () => import('@/views/redPacketConfig/index'),
        meta: { title: '红包配置' }
      }
    ]
  },
  { path: '*', redirect: '/' }
];
const router = new Router({ whiteList, routes });
document.title = '万象掌柜';
router.beforeEach((to, from, next) => {
  const hasToken = store.state.token;
  if (hasToken) {
    next();
  } else {
    if (router.options.whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
    }
  }
});
export default router;
